<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\9\10 0010
 * Time: 17:42
 */

namespace app\Lib\Model\AdminAuth;


use app\Lib\Exception\BlException;
use app\Lib\InterfaceIn\AdminAuth\MenuAuthHandle;
use app\Lib\InterfaceIn\User\UserHandle;
use app\Lib\Model\DbModel\AdminMenu;
use app\Lib\Model\User\User;

class MenuAuth implements MenuAuthHandle
{
    private $menuId;
    private $cachePrefix ;
    private $Db;
    private $cache;
    public function __construct($menuId,$cache = true)
    {
        $this->menuId = $menuId;
        $this->cache  = $cache;
        $this->cachePrefix = 'menu_Id_'.$this->menuId.'_';
        $this->Db = new AdminMenu();
    }

    /**
     * 菜单id
     * @return int
     */
    public function getMenuId()
    {
        return $this->menuId;
    }

    /**
     * 菜单名称
     * @return string
     */
    public function getName()
    {
        return  $this->getFieldValue($this->Db->_name);
    }

    /**
     * 项目模块
     * @return string
     */
    public function getProject()
    {
        return  $this->getFieldValue($this->Db->_project);
    }

    /**
     * 菜单控制器
     * @return string
     */
    public function getC()
    {
        return  $this->getFieldValue($this->Db->_c);
    }

    /**
     * 菜单方法
     * @return string
     */
    public function getA()
    {
        return  $this->getFieldValue($this->Db->_a);
    }

    /**
     * 菜单参数
     * @return string
     */
    public function getArgs()
    {
        return  $this->getFieldValue($this->Db->_args);
    }

    /**
     * 菜单父级类
     * @return MenuAuth|int
     */
    public function getParentClass()
    {
        $parentId =  $this->getFieldValue($this->Db->_parent_id);
        //if($parentId>0){
        return new MenuAuth($parentId);
        //}
    }

    /**
     * 菜单等级
     * @return int
     */
    public function getLevel()
    {
        return $this->getFieldValue($this->Db->_level);
    }

    /**
     * 菜单排序
     * @return int
     */
    public function getSortNo()
    {
        return $this->getFieldValue($this->Db->_sort_no);
    }

    /**
     * 菜单是否显示状态中文
     * @return string
     */
    public function getShowMenuStr()
    {
        $chineseData = [0=>'隐藏',1=>'显示'];
        $status = $this->getShowMenu();
        return isset($chineseData[$status])?$chineseData[$status]:'未知';
    }

    /**
     * 菜单图标样式
     * @return string
     */
    public function getMenuClass()
    {
        return $this->getFieldValue($this->Db->_menu_class);
    }

    /**
     * 菜单状态中文
     * @return string
     */
    public function getStatusStr()
    {
        $chineseData = [0=>'禁用',1=>'启用'];
        $status = $this->getMenuStatus();
        return isset($chineseData[$status])?$chineseData[$status]:'未知';
    }

    /**
     * 创建用户类
     * @return UserHandle
     */
    public function getCreateUserClass()
    {
        $uid = $this->getFieldValue($this->Db->_create_uid);
        return new User($uid);
    }

    /**
     * 创建时间
     * @return int
     */
    public function getCreateTime()
    {
        return $this->getFieldValue($this->Db->_create_time);
    }

    /**
     * 修改用户类
     * @return UserHandle
     */
    public function getSaveUserClass()
    {
        $uid = $this->getFieldValue($this->Db->_save_uid);
        return new User($uid);
    }

    /**
     * 修改时间
     * @return int
     */
    public function getSaveTime()
    {
        return $this->getFieldValue($this->Db->_save_time);
    }

    /**
     * 删除类的缓存
     * @return bool
     * @throws BlException
     */
    public function delAllCache()
    {
        $result =  $this->Db->getTableFields();
        foreach ($result as $val){
            $this->delSingleCache($val);
        }
        return true;
    }

    /**
     * 删除单个字段缓存
     * @param $field
     * @return bool
     * @throws BlException
     */
    public function delSingleCache($field)
    {
        if(empty($field)){
            throw new BlException('请提供要清除缓存的字段');
        }
        $cacheName = $this->getCacheName($field);
//        throw new BlException($cacheName);
        cache($cacheName,null);
        return true;
    }
    /**
     * 获取某个字段的值
     * @param $filed
     * @return string
     */
    private function getFieldValue($filed){
        $result = false;
        if($this->cache){
            $result = $this->getCacheValue($filed);
        }
        if(!$result){
            $result = $this->getDbFiledValue($filed);
        }
        return $result;
    }
    private function getCacheValue($filed){
        $result = cache($this->getCacheName($filed));
        if(empty($result)){
            $result =$this->getDbFiledValue($filed);
            $result = empty($result)?'无':$result;
            cache($this->getCacheName($filed),$result);
        }
        return $result;
    }

    private function getDbFiledValue($filed){
        return $this->Db->where($this->Db->_id,$this->menuId)->value($filed);
    }

    /**
     * 获取缓存名称
     * @param $filed
     * @return string
     */
    private function getCacheName($filed){
        return $this->cachePrefix.$filed;
    }

    /**
     * 获取菜单状态
     * @return int
     */
    public function getMenuStatus(){
        return $this->getFieldValue($this->Db->_status);
    }

    /**
     * 获取菜单显示状态
     * @return int
     */
    public function getShowMenu(){
        return $this->getFieldValue($this->Db->_show_menu);
    }

    /**
     * 菜单标识
     * @return string
     */
    public function getMark(){
        return $this->getFieldValue($this->Db->_mark);
    }


}